package com.hjs.tools;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author violetfreesia
 * @date 2020/06/17
 */
@Slf4j
public class RequestUtil {
    final HttpServletRequest request;
    final HttpServletResponse response;

    public RequestUtil(HttpServletRequest request) {
        this.request = request;
        this.response = null;
    }

    public RequestUtil(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    public String getString(String name) {
        return getString(request, name);
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    public Integer getInt(String name) {
        return getInt(request, name);
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    public Double getDouble(String name) {
        return getDouble(request, name);
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    public Boolean getBoolean(String name) {
        return getBoolean(request, name);
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    public LocalDate getDate(String name) {
        return getDate(request, name);
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    public LocalDateTime getDateTime(String name) {
        return getDateTime(request, name);
    }

    /**
     * 获取打印流对象
     *
     * @return 打印流对象
     */
    public PrintWriter getPrintWriter() {
        if (response == null) {
            log.error("未传入HttpServletResponse对象");
            throw new NullPointerException("构建该对象时未传入HttpServletResponse对象引起的空指针异常");
        }
        return getPrintWriter(response);
    }

    /**
     * 向前端返回字符串
     *
     * @param result 要返回的结果
     */
    public void responseWrite(String result) {
        if (response == null) {
            log.error("未传入HttpServletResponse对象");
            throw new NullPointerException("构建该对象时未传入HttpServletResponse对象引起的空指针异常");
        }
        responseWrite(response, result);
    }

    /**
     * 向前端返回结果
     *
     * @param result result
     */
    @Deprecated(forRemoval = true)
    public void responseIsSuccess(boolean result) {
        String re = "{\"result\": " + result + "}";
        responseWrite(response, re);
    }

    /**
     * 返回带错误信息得结果
     *
     * @param result   result
     * @param errorMsg 错误信息
     */
    public void responseIsSuccess(boolean result, String errorMsg, int errorCode) {
        String re = "{\"result\": true,\"code\": 2000}";
        if (!result) {
            re = "{\"result\": false,\"code\": " + errorCode + ",\"errorMsg\": \"" + errorMsg + "\"}";
        }
        responseWrite(response, re);
    }

    public void responseIsSuccess(boolean result, String errorMsg) {
        responseIsSuccess(result, errorMsg, 5000);
    }

    /**
     * 向前端返回成功
     */
    public void responseSuccess() {
        responseIsSuccess(true, null);
    }

    /**
     * 向前端返回失败
     */
    @Deprecated(forRemoval = true)
    public void responseFail() {
        responseIsSuccess(false);
    }

    public void responseFail(String errorMsg) {
        responseIsSuccess(false, errorMsg);
    }

    public void responseFail(String errorMsg, int errorCode) {
        responseIsSuccess(false, errorMsg, errorCode);
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    public static String getString(HttpServletRequest request, String name) {
        return request.getParameter(name);
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    private static String getNotNullParameter(String name, String parameter) {
        if (parameter == null) {
            log.error("未获取到名为" + name + "的参数");
            throw new NullPointerException("由于未获取到参数引起的空指针异常");
        }
        return parameter;
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    public static Integer getInt(HttpServletRequest request, String name) {
        String parameter = getNotNullParameter(name, getString(request, name));
        return Integer.parseInt(parameter);
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    public static Double getDouble(HttpServletRequest request, String name) {
        String parameter = getNotNullParameter(name, getString(request, name));
        return Double.parseDouble(parameter);
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    public static Boolean getBoolean(HttpServletRequest request, String name) {
        String parameter = getNotNullParameter(name, getString(request, name));
        return Boolean.parseBoolean(parameter);
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    public static LocalDate getDate(HttpServletRequest request, String name) {
        String parameter = getNotNullParameter(name, getString(request, name));
        return LocalDate.parse(parameter);
    }

    /**
     * 根据参数名返回request中对应类型的值
     *
     * @param name 参数名
     * @return 值
     */
    public static LocalDateTime getDateTime(HttpServletRequest request, String name) {
        String parameter = getNotNullParameter(name, getString(request, name));
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        return LocalDateTime.parse(parameter, dateTimeFormatter);
    }

    /**
     * 设置response的字符编码
     *
     * @param response response
     */
    public static void setResponse(HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=UTF-8");
    }

    /**
     * 获取打印流对象
     *
     * @param response response
     * @return 打印流对象
     */
    public static PrintWriter getPrintWriter(HttpServletResponse response) {
        setResponse(response);
        PrintWriter writer;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            log.error("获取打印流对象失败");
            throw new NullPointerException("获取打印流对象失败引起的空指针异常");
        }
        return writer;
    }

    /**
     * 向前端返回字符串
     *
     * @param response response
     * @param result   要返回的结果
     */
    public static void responseWrite(HttpServletResponse response, String result) {
        PrintWriter printWriter = getPrintWriter(response);
        printWriter.write(result);
    }

    /**
     * 向前端返回结果
     *
     * @param response response
     * @param result   result
     */
    @Deprecated(forRemoval = true)
    public static void responseIsSuccess(HttpServletResponse response, boolean result) {
        String re = "{\"result\": " + result + "}";
        responseWrite(response, re);
    }

    /**
     * 返回带错误信息得结果
     *
     * @param response response
     * @param result   result
     * @param errorMsg 错误信息
     */
    public static void responseIsSuccess(HttpServletResponse response, boolean result, String errorMsg) {
        String re = "{result: true}";
        if (!result) {
            re = "{result: false\nerrorMsg: " + errorMsg + "}";
        }
        responseWrite(response, re);
    }

    /**
     * 向前端返回成功
     *
     * @param response response
     */
    public static void responseSuccess(HttpServletResponse response) {
        responseIsSuccess(response, true);
    }

    /**
     * 向前端返回失败
     *
     * @param response response
     */
    public static void responseFail(HttpServletResponse response) {
        responseIsSuccess(response, false);
    }

}
